const { Sequelize, Model, INTEGER } = require('sequelize')
const { db } = require('../core/sequelize')
const moment = require('moment')
class TaskJournal extends Model {
    static async creatTaskJournal(taskId, userId) {
        let result = await TaskJournal.findCreateFind({
            // 没有 就插入一条数据
            defaults: {
                taskId: taskId,
                userId: userId,
                journalTime: moment().format("YYYY-MM-DD")
            },
            // 查询是否已经签到
            where: {
                task_id: taskId,
                user_id: userId,
                journal_time: moment().format("YYYY-MM-DD")

            }
        })
        return result
    }
    //static关键字静态方法 不用new 可以直接调用
    static async getList(userId, journalTime, taskId) {
        // where查询
        const taskList = await TaskJournal.findAll({
            where: {
                userId,
                journalTime,
                taskId
            }
        })
        return taskList
    }
}
TaskJournal.init(
    {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true, //设置主键
            autoIncrement: true,
        },
        journalTime: Sequelize.STRING,
        userId: Sequelize.INTEGER,
        taskId: Sequelize.INTEGER,
    },
    {
        // underscored: true,
        sequelize: db,
        modelName: 'taskJournal', timestamps: false
    }
)
module.exports = { TaskJournal }
